package com.infinit.easyreader.Biz;

import android.content.Context;
import android.content.SharedPreferences;

import com.infinit.easyreader.entity.PreferencesSettings;
import com.infinit.easyreader.ui.FontActivity;
import com.infinit.easyreader.utils.Consts;

/**
 * 类：com.infinit.easyreader.Biz.PreferenceBiz V[1.0.0]
 * 作用：处理Preference的读取和保存
 * Created by rzy on 2016/7/5.
 */
public class PreferenceBiz implements Consts {

    private SharedPreferences preferences;
    private PreferencesSettings settings;

    public PreferenceBiz(Context context) {
        settings = new PreferencesSettings();
        preferences = context.getSharedPreferences(settings.getPreferenceName(), Context.MODE_PRIVATE);
    }

    /**
     * 读取Preferences中的设置
     */

    public PreferencesSettings readPreferences() {

        settings.setAutoRead(preferences.getBoolean(SETTING_ISAUTOREAD, true));
        settings.setAutoBrightness(preferences.getBoolean(SETTING_ISAUTOREAD, false));
        settings.setBgResID(preferences.getInt(SETTING_BG, 0));
        settings.setBrightness(preferences.getInt(SETTING_BRIGHTNESS, 0));
        settings.setFontFamily(preferences.getString(SETTING_FONTFAMINY, FontActivity.FONT_SYS));
        settings.setFontSize(preferences.getInt(SETTING_FONTSIZE, -1));
        settings.setLastBrightness(preferences.getInt(SETTING_LAST_BRIGHTNESS, -1));
        return settings;

    }

    /**
     * 保存到Preference中
     */

    public void savePreferences(PreferencesSettings settings) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(SETTING_ISAUTOREAD, settings.isAutoRead());
        editor.putBoolean(SETTING_ISAUTOBRIGHTNESS, settings.isAutoBrightness());
        editor.putInt(SETTING_BG, settings.getBgResID());
        editor.putInt(SETTING_BRIGHTNESS, settings.getBrightness());
        editor.putString(SETTING_FONTFAMINY, settings.getFontFamily());
        editor.putInt(SETTING_FONTSIZE, settings.getFontSize());
        editor.putInt(SETTING_LAST_BRIGHTNESS, settings.getLastBrightness());
        editor.apply();

    }


}
